/* CHUONG TRINH QUAN LY CUA HANG SACH
PHAN GIAO DIEN DANH CHO NHAN VIEN QUAN LY
* Chuong trinh su dung cac ham:
 Kiem tra dang nhap bang tai khoan he thong
 Tim kiem, sua, xoa thong tin ve cuon sach
 Kiem tra doanh thu ban hang
 Tim kiem khach hang bang email dang ky
*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include "MyFunc.c"
#include "DeclearFunc.c"


void main() {
	//=> Dang nhap
	login();
	//=> Menu
	int choose;
	char email[255], chooseYN;
	while(1) {
		cls();
		printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
		printf("\t\t    CHUONG TRINH DANH CHO NHAN VIEN QUAN LY\n\n");
		printf("\t\t\t\tMENU CHUC NANG \n\n");
		printf("\t\t+=========================================+\n\n");
		printf("\t\t1. Tim kiem thong tin khach hang\n\n");
		printf("\t\t2. Quan ly danh muc sach\n\n");
		printf("\t\t3. Quan ly doanh thu ban hang\n\n");
		printf("\t\t4. Thoat ung dung\n\n");
		printf("\t\t+=========================================+\n\n");
		printf("\t\t*Chu y: Nhap so ung voi moi chuc nang!\n\n");
		printf("\t\tMoi ban chon chuc nang: ");
		scanf("%d", &choose);
		switch(choose) {
			//=> Quan Ly thong tin khach hang
			case 1:
				while(1) {
					cls();
					printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
					printf("\t\t\t QUAN LY THONG TIN KHACH HANG\n\n");
					printf("\t\tNhap email khach hang can xem thong tin: \n\n\t\t");
					scanf("%s", email);
					showUser(email);
					printf("\tBan muon tim kiem tiep khong?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'n' || chooseYN == 'N') {
						break;
					}
				}
				break;
			//=> Quan Ly danh muc sach
			case 2:
				menuManagerBook();
				break;
			//=> Quan ly doanh thu ban hang
			case 3:
				menuStatistics();
				break;
			//=> Thoat
			case 4:
				exit(0);
			//=> In thong bao neu nhap sai so
			default:
				printf("\n\t\tNhap so tu 1 -> 4. Nhan phim bat ky de nhap lai...");
				flushCache(0);
		}
	}
	flushCache(2);
}

//=> Giao dien phan dang nhap
void login() {
	char user[255], pass[255];
	int i = 0, isValidMail, isValidPass;
	while(1) {
		system("cls");
		printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
		printf("\t\tNhap tai khoan cua he thong de dang nhap\n\n");
		printf("\t\t+=========================================+\n");
		printf("\n\t\tUser: ");
		scanf("%s", user);
		printf("\n\t\tPassword: ");
		scanf("%s", pass);
		printf("\n\t\t+=========================================+\n");
		isValidMail = checkMail(user, 1);
		isValidPass = checkPW(pass, 1);
		if(isValidMail && isValidPass) {
			break;
		}
		printf("\t\tTai khoan hoac mat khau khong dung. Moi ban nhap lai");
		flushCache(0);
	}
}

//=> Quan ly danh muc sach
void menuManagerBook() {
	bookInfo bookInfo;
	int choose, isValidBook;
	char nameOfBook[255], chooseYN;
	while(choose != 5) {
		cls();
		printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
		printf("\t\t\t\tQUAN LY THONG TIN SACH\n\n");
		printf("\t\t+===================================+\n\n");
		printf("\t\t1. Them sach vao kho thu vien\n\n");
		printf("\t\t2. Sua thong tin sach\n\n");
		printf("\t\t3. Xoa thong tin ve cuon sach\n\n");
		printf("\t\t4. Tim kiem thong tin ve cuon sach\n\n");
		printf("\t\t5. Quay ve Menu chinh\n\n");
		printf("\t\t+===================================+\n\n");
		printf("\t\tMoi ban chon chuc nang: ");
		scanf("%d", &choose);
		switch(choose) {
			//=> Them sach vao kho thu vien
			case 1:
				while(1) {
					cls();
					printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
					printf("\t\t\t\tTHEM SACH VAO THU VIEN\n\n");
					printf("\t\t     ------------------------------------\n\n");
					printf("\t\tNhap thong tin ve cuon sach\n\n");
					printf("\tTen sach: ");
					fflush(stdin);
					gets(bookInfo.name);
					printf("\n\tTac gia: ");
					gets(bookInfo.author);
					fflush(stdin);
					printf("\n\tNXB: ");
					gets(bookInfo.publisher);
					fflush(stdin);
					printf("\n\tNam xuat ban: ");
					scanf("%d", &bookInfo.releaseYear);
					printf("\n\tGia: ");
					scanf("%d", &bookInfo.price);
					isValidBook = addBook(bookInfo.name, bookInfo.author, bookInfo.publisher,
											 bookInfo.releaseYear, bookInfo.price);
					if(isValidBook) {
						printf("\n\t*Chuc mung! Ban da them thanh cong.*");
					} else {
						printf("\n\t*Cuon sach da co trong thu vien!*");
					}
					flushCache(1);
					printf("\n\n\tBan muon nhap them sach?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'N' || chooseYN == 'n') {
						break;
					}
				}
				flushCache(1);
				break;
			//=> Sua sach trong thu vien
			case 2:
				while(1) {
					cls();
					printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
					printf("\t\t\t\tSUA SACH TRONG THU VIEN\n\n");
					printf("\t\t     ------------------------------------\n\n");
					printf("\t\tNhap ten cuon sach can sua: ");
					fflush(stdin);
					gets(bookInfo.name);
					isValidBook = findFile(bookInfo.name, LINK_LIBRARY);
					if(isValidBook) {
						printf("\n\n\tNhap lai thong tin cuon sach");
						printf("\n\n\tTac gia: ");
						gets(bookInfo.author);
						fflush(stdin);
						printf("\n\tNXB: ");
						gets(bookInfo.publisher);
						fflush(stdin);
						printf("\n\tNam xuat ban: ");
						scanf("%d", &bookInfo.releaseYear);
						printf("\n\tGia: ");
						scanf("%d", &bookInfo.price);
						isValidBook = editBook(bookInfo.name, bookInfo.author, bookInfo.publisher,
											 bookInfo.releaseYear, bookInfo.price);
						if(isValidBook) {
							printf("\n\t*Chuc mung! Ban da sua thanh cong.*");
						} else{
							printf("\n\t*Sua that bai!*");
						}
					} else {
						printf("\n\t*Cuon sach khong co trong thu vien!*");
					}
					flushCache(1);
					printf("\n\n\tBan muon nhap them sach?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'N' || chooseYN == 'n') {
						break;
					}
				}
				flushCache(1);
				break;
			//=> Xoa thong tin sach
			case 3:
				while(1) {
					cls();
					printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
					printf("\t\t\tXOA SACH TRONG THU VIEN\n\n");
					printf("\t\t     ------------------------------------\n\n");
					fflush(stdin);
					printf("\t\tNhap ten sach can xoa: ");
					gets(nameOfBook);
					isValidBook = removeBook(nameOfBook);
					if(isValidBook) {
						printf("\n\n\t\t*Xoa thanh cong!*");
					} else {
						printf("\n\n\t\t*Khong tim thay sach trong thu vien!*");
					}
					flushCache(1);
					printf("\n\n\tBan muon xoa them sach?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'N' || chooseYN == 'n') {
						break;
					}
				}
				flushCache(1);
				break;
			//=> Tim kiem sach
			case 4:
				while(1) {
					cls();
					printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
					printf("\t\t\tTIM KIEM SACH TRONG THU VIEN\n\n");
					printf("\t\t     ------------------------------------\n\n");
					printf("\t\tNhap ten sach can xem thong tin: \n\n\t\t");
					fflush(stdin);
					gets(nameOfBook);
					showOneBook(nameOfBook);
					printf("\n\n\tBan muon tim kiem them sach?(Y/N): ");
					scanf("%c", &chooseYN);
					if(chooseYN == 'N' || chooseYN == 'n') {
						break;
					}
				}
				flushCache(1);
			//=>Thoat menu
			case 5:
				break;
			//=> Hien thong bao neu nhap sai
			default:
				printf("\n\t\tNhap so tu 1 -> 5. Nhan phim bat ky de nhap lai...");
				flushCache(0);
		}
	}
}

//=> Quan Ly doanh thu ban hang
void menuStatistics() {
	int choose;
	char day[3], month[3], year[5], chooseYN;
	while(choose != 4) {
		cls();
		printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
		printf("\t\t\tQUAN LY DOANH THU BAN HANG\n\n");
		printf("\t\t+===================================+\n\n");
		printf("\t\t1. Xem doanh thu theo ngay\n\n");
		printf("\t\t2. Xem doanh thu theo thang\n\n");
		printf("\t\t3. Xem doanh thu theo nam\n\n");
		printf("\t\t4. Quay ve Menu chinh\n\n");
		printf("\t\t+===================================+\n\n");
		printf("\t\tMoi ban chon chuc nang: ");
		scanf("%d", &choose);
		switch(choose) {
			//=> Doanh thu theo ngay
		 	case 1:
				cls();
				printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
				printf("\t\t      QUAN LY DOANH THU BAN HANG THEO NGAY\n\n");
				printf("\t\t-----------------------------------------\n\n");
				printf("\t\tNhap du lieu ngay thang nam\n\n");
				printf("\t\tNhap Nam: ");
				scanf("%s", year);
				fflush(stdin);
				printf("\n\t\tNhap Thang: ");
				scanf("%s", month);
				fflush(stdin);
				printf("\n\t\tNhap ngay: ");
				scanf("%s", day);
				printf("\n\n\t\t-----------------------------------------");
				printf("\n\n\t\tDoanh thu ban hang trong ngay %s thang %s nam %s cua ban la\n\n\t\t%d VND", day, month, year, statisticsRevenue(year, month, day, 3));
				flushCache(1);
				break;
			//=> Doanh thu theo thang
			case 2:
				cls();
				printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
				printf("\t\t      QUAN LY DOANH THU BAN HANG THEO THANG\n\n");
				printf("\t\t-----------------------------------------\n\n");
				printf("\t\tNhap du lieu thang nam\n\n");
	   			printf("\t\tNhap Nam: ");
				scanf("%s", year);
				fflush(stdin);
				printf("\n\t\tNhap Thang: ");
				scanf("%s", month);
				printf("\n\t\t-----------------------------------------");
				printf("\n\n\t\tDoanh thu ban hang trong thang %s nam %s cua ban la\n\n\t\t%d VND", month, year, statisticsRevenue(year, month, day, 2));
				flushCache(1);
				break;
			//=> Doanh thu theo nam
			case 3:
				cls();
				printf("\n\t\t========*UNG DUNG QUAN LY CUA HANG SACH*========\n\n");
				printf("\t\t      QUAN LY DOANH THU BAN HANG THEO NAM\n\n");
				printf("\t\t-----------------------------------------\n\n");
	   			printf("\t\tNhap Nam: ");
				scanf("%s", year);
				fflush(stdin);
				printf("\n\t\t-----------------------------------------");
				printf("\n\n\t\tDoanh thu ban hang trong nam %s cua ban la\n\n\t\t%d VND", year, statisticsRevenue(year, month, day, 1));
				flushCache(1);
				break;
			//=> Quay ve menu chinh
			case 4:
				break;
			//=> Hien thong bao neu nhap sai
			default:
				printf("\n\t\tNhap so tu 1 -> 4. Nhan phim bat ky de nhap lai...");
				flushCache(0);
		}
	}
}

